package com.csun.dsms.utils;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

public class YcRequestUtil {

    /**
     * GET请求.
     * @param url 请求地址
     * @return 响应数据
     */
    public static String doYcLogin(String url) {
        HttpURLConnection urlConnection = null;
        try {
            // 新建URL对象
            URL urlObject = new URL(url);
            // 打开URL连接
            urlConnection = (HttpURLConnection) urlObject.openConnection();
            // 设置请求方法
            urlConnection.setRequestMethod("GET");
            // 设置请求头内容类型和字符集类型
            urlConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
            // 不使用缓存
            urlConnection.setUseCaches(false);
            // 设置连接超时时间，单位：毫秒
            urlConnection.setConnectTimeout(2000);
            // 设置读取数据超时时间，单位：毫秒
            urlConnection.setReadTimeout(2000);
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String body = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
            if (urlConnection.getResponseCode() == 200){
                return body;
            }else{
                return "{\"code\": \"-1\", \"msg\":\"接口异常\"，\"data\": \"\"}";
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * GET请求.
     * @param url 请求地址
     * @return 响应数据
     */
    public static String doYcGet(String url,String token) {
        HttpURLConnection urlConnection = null;
        try {
            // 新建URL对象
            URL urlObject = new URL(url);
            // 打开URL连接
            urlConnection = (HttpURLConnection) urlObject.openConnection();
            // 设置请求方法
            urlConnection.setRequestMethod("GET");
            // 设置请求头内容类型和字符集类型
            urlConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("token",token);
            // 不使用缓存
            urlConnection.setUseCaches(false);
            // 设置连接超时时间，单位：毫秒
            urlConnection.setConnectTimeout(2000);
            // 设置读取数据超时时间，单位：毫秒
            urlConnection.setReadTimeout(2000);
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String body = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
            if (urlConnection.getResponseCode() == 200){
                return body;
            }else{
                return "{\"code\": \"-1\", \"msg\":\"接口异常\"，\"data\": \"\"}";
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }



}
